package leetcode_301_400;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

public class LeeCode_365 {
    public static void main(String[] args) {

    }

    public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
        if (targetCapacity > jug1Capacity + jug2Capacity){
            return false;
        }
        double m1 = Math.pow(jug1Capacity, 0.5);
        Set<Integer> set = new HashSet<>();
        for (int i = 1; i <= m1; i++) {
            if (jug1Capacity % i == 0){
                set.add(i);
                set.add(jug1Capacity / i);
            }
        }
        double m2 = Math.pow(jug2Capacity, 0.5);
        TreeSet<Integer> tree = new TreeSet<>();
        for (int i = 1; i <= m2; i++) {
            if (jug2Capacity % i == 0){
                if (set.contains(i)){
                    tree.add(i);
                }
                if (set.contains(jug2Capacity / i)){
                    tree.add(jug2Capacity / i);
                }
            }
        }
        return targetCapacity % tree.last() == 0;
    }
}
